package com.jf3q.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.admin.entity.User;
import com.jf3q.admin.mapper.RoleMapper;
import com.jf3q.admin.mapper.UserMapper;
import com.jf3q.admin.utils.dto.Eacharts;
import com.jf3q.admin.utils.dto.EchartsDto;
import com.jf3q.admin.utils.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserDetailsService {

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> userWrapper=new LambdaQueryWrapper<>();
        userWrapper.eq(User::getUsername,username);
        User user = this.getOne(userWrapper);
        if (user==null){
            throw new RuntimeException("用户不存在！");
        }
        user.setRoles(roleMapper.selectByUserId(user.getId()));
        return user;
    }

    public List<MenuVo> getMyMenu(String username) {
        return userMapper.selMyMenu(username);
    }

    public EchartsDto groupByTeacher() {

        EchartsDto echartsDto= new EchartsDto();
        echartsDto.setTeacherName(new ArrayList<>());
        echartsDto.setPlayCount(new ArrayList<>());

        List<Eacharts> list =userMapper.groupByTeacher();
        list.stream().map(
                m ->{
                    echartsDto.getTeacherName().add( m.getName());
                    echartsDto.getPlayCount().add( m.getTotal());
                    return m;
                }
        ).collect(Collectors.toList());


        return echartsDto;
    }
}




